package test;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import sun.misc.BASE64Decoder;

public class ExportImage {

	public static void main(String[] args) {
//		String a = "ͼƬ:iVBORw0KGgoAAAANSUhEUgAAASwAAACWCAYAAABkW7XSAAAEYklEQVR4Xu3UAQkAAAwCwdm/9HI83BLIOdw5AgQIRAQWySkmAQIEzmB5AgIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlAABg+UHCBDICBisTFWCEiBgsPwAAQIZAYOVqUpQAgQMlh8gQCAjYLAyVQlKgIDB8gMECGQEDFamKkEJEDBYfoAAgYyAwcpUJSgBAgbLDxAgkBEwWJmqBCVAwGD5AQIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlAABg+UHCBDICBisTFWCEiBgsPwAAQIZAYOVqUpQAgQMlh8gQCAjYLAyVQlKgIDB8gMECGQEDFamKkEJEDBYfoAAgYyAwcpUJSgBAgbLDxAgkBEwWJmqBCVAwGD5AQIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlAABg+UHCBDICBisTFWCEiBgsPwAAQIZAYOVqUpQAgQMlh8gQCAjYLAyVQlKgIDB8gMECGQEDFamKkEJEDBYfoAAgYyAwcpUJSgBAgbLDxAgkBEwWJmqBCVAwGD5AQIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlAABg+UHCBDICBisTFWCEiBgsPwAAQIZAYOVqUpQAgQMlh8gQCAjYLAyVQlKgIDB8gMECGQEDFamKkEJEDBYfoAAgYyAwcpUJSgBAgbLDxAgkBEwWJmqBCVAwGD5AQIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlAABg+UHCBDICBisTFWCEiBgsPwAAQIZAYOVqUpQAgQMlh8gQCAjYLAyVQlKgIDB8gMECGQEDFamKkEJEDBYfoAAgYyAwcpUJSgBAgbLDxAgkBEwWJmqBCVAwGD5AQIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlAABg+UHCBDICBisTFWCEiBgsPwAAQIZAYOVqUpQAgQMlh8gQCAjYLAyVQlKgIDB8gMECGQEDFamKkEJEDBYfoAAgYyAwcpUJSgBAgbLDxAgkBEwWJmqBCVAwGD5AQIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlAABg+UHCBDICBisTFWCEiBgsPwAAQIZAYOVqUpQAgQMlh8gQCAjYLAyVQlKgIDB8gMECGQEDFamKkEJEDBYfoAAgYyAwcpUJSgBAgbLDxAgkBEwWJmqBCVAwGD5AQIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlAABg+UHCBDICBisTFWCEiBgsPwAAQIZAYOVqUpQAgQMlh8gQCAjYLAyVQlKgIDB8gMECGQEDFamKkEJEDBYfoAAgYyAwcpUJSgBAgbLDxAgkBEwWJmqBCVAwGD5AQIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlAABg+UHCBDICBisTFWCEiBgsPwAAQIZAYOVqUpQAgQMlh8gQCAjYLAyVQlKgIDB8gMECGQEDFamKkEJEDBYfoAAgYyAwcpUJSgBAgbLDxAgkBEwWJmqBCVAwGD5AQIEMgIGK1OVoAQIGCw/QIBARsBgZaoSlACBB1YxAJfjJb2jAAAAAElFTkSuQmCC";
//		GenerateImage(a,"tt.png","");
		GenerateImageForMysql();
//		GenerateImageForOracle()
	}
	
	private static void  GenerateImageForMysql() 
	{
		try {
			// 加载MySql的驱动类
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://172.16.3.4:3306/szgawx?user=root&password=Admin_szga";
			String username = "root";
			String password = "Admin_szga";
			Connection con = DriverManager.getConnection(url, username,
					password);
			String sql = "select  * from wx_owner t where uid='B64B922ABD014B05A0284DAC12ADA98B'";
			Statement stmt = con.createStatement();
			PreparedStatement pstmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				// System.out.print(rs.getString("filecontent") + " ");
				GenerateImage(rs.getString("qztxstr"),
						"test1.png", rs.getString("uid"));
				GenerateImage(rs.getString("qzsfzzpstr"),
						"test2.png", rs.getString("uid"));
				GenerateImage(rs.getString("fczzpstr"),
						"test3.png", rs.getString("uid"));
//				GenerateImage(rs.getString("qzzpstr"),
//						"test3.png", rs.getString("uid"));
//				GenerateImage(rs.getString("myzzpstr"),
//						"test4.png", rs.getString("uid"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	private static void  GenerateImageForOracle()
	{
		try {
			// 加载MySql的驱动类
			Class.forName("oracle.jdbc.OracleDriver");
			String url = "jdbc:oracle:thin:@//172.16.2.101:1521/orcl";
			String username = "PASC_JSSZ";
			String password = "PASC_JSSZ";
			Connection con = DriverManager.getConnection(url, username,
					password);
			String sql = "select  * from ASC_PROJECTATTACHMENTS_att c inner join ASC_PROJECTATTACHMENTS  a "
					+ " on c.guid=a.floderguid right join ASC_PROJECTMATERIAL b on a.bizguid=b.lguid "
					+ " where b.projectguid='65e47669-af3b-4de6-8e1f-58d6f5d99336' ";
			Statement stmt = con.createStatement();
			PreparedStatement pstmt = con.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				// System.out.print(rs.getString("filecontent") + " ");
				GenerateImage(rs.getString("filecontent"),
						rs.getString("filename"), rs.getString("contenttype"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	private static boolean GenerateImage(String imgStr, String name, String type) { // 对字节数组字符串进行Base64解码并生成图片
		if (imgStr == null) // 图像数据为空
			return false;
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			// Base64解码
			byte[] b = decoder.decodeBuffer(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			// 生成jpeg图片
			String imgFilePath = "C://" + name;// 新生成的图片
			OutputStream out = new FileOutputStream(imgFilePath);
			out.write(b);
			out.flush();
			out.close();
			System.out.println("已生成:" + imgFilePath);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
